package cn.rokhdelar.umbrsapi.test;

import cn.rokhdelar.umbrsapi.core.dto.PageResult;
import cn.rokhdelar.umbrsapi.core.entity.BasePageDto;
import cn.rokhdelar.umbrsapi.core.jpa.nativesql.QueryHelper;
import cn.rokhdelar.umbrsapi.core.service.impl.BaseServiceImpl;
import cn.rokhdelar.umbrsapi.core.utils.EscapeUtil;
import cn.rokhdelar.umbrsapi.management.entity.SysUser;
import cn.rokhdelar.umbrsapi.management.repository.SysUserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: umbrs-api-tool
 * @BelongsPackage: cn.rokhdelar.umbrsapi.test
 * @Author: shenyongqiang
 * @CreateTime: 2024-06-12  10:23
 * @Description: TODO
 * @Version: 1.0
 */
@Service(value = "studentService")
public class StudentServiceImpl extends BaseServiceImpl<Student> implements StudentService{
    @Override
    protected Class<Student> getEntityClass() {
        return Student.class;
    }

    @Autowired
    private SysUserRepo sysUserRepo;

    @Override
    public PageResult<Student> pageHelper(BasePageDto dto) {
        QueryHelper queryHelper = new QueryHelper("select s.address,s.leader,s.name,a.password ","student s " +
                " left join  sys_user a on s.id=a.id");
        queryHelper.addCondition(" s.name like ? ","%"+ EscapeUtil.escapeChar("好") +"%");
        queryHelper.addLimit(0,1);
        List listBySql = getBaseSerice().findListBySql(queryHelper);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savestudent(Student student) {
        doInsert(student);
//        int a=1/0;
        SysUser sysUser = new SysUser();
        sysUser.setId(100l);
        sysUser.setPassword("12345");
        sysUserRepo.save(sysUser);
    }

    @Override
    public List<Map<String, Object>> ListSqlHelper(BasePageDto dto) {
        QueryHelper queryHelper = new QueryHelper("select s.address,count(*) ","student s " +
                " left join  sys_user a on s.id=a.id");
        queryHelper.addCondition(" s.name like ? ","%"+ EscapeUtil.escapeChar("好") +"%");
        queryHelper.addGroupProperty("s.address");
        List listBySql = getBaseSerice().findListBySql(queryHelper);
        return  listBySql;
    }
}
